﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;


namespace M
{
    public class Http
    {
        public static string GetAndReturnHTML(string url)
        {
            try
            {
                Uri uri = new Uri(url);

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                //request.Timeout = 1000 * NetworkMethods.GetHTMLRequestTimeout();
                request.Method = WebRequestMethods.Http.Get;
                request.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string tmp = reader.ReadToEnd();
                response.Close();
                return tmp;
            }
            catch (Exception ekkk)
            {
                return "";
            }
        }
        public static string GetAndReturnHTML(string url, CookieCollection cookies)
        {
            try
            {
                Uri uri = new Uri(url);

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                //request.Timeout = 1000 * NetworkMethods.GetHTMLRequestTimeout();
                request.Method = WebRequestMethods.Http.Get;
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string tmp = reader.ReadToEnd();
                response.Close();
                return tmp;
            }
            catch (Exception ekkk)
            {
                return "";
            }
        }
        public static CookieCollection GetAndReturnCookies(string url)
        {
            Uri uri = new Uri(url);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            //request.Timeout = 1000 * NetworkMethods.GetHTMLRequestTimeout();
            request.Method = WebRequestMethods.Http.Get;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();
            response.Close();
            return response.Cookies;
        }

        public static string PostAndReturnHTML_OK(string url, string data)
        {
            string postData = data;
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            //request.Timeout = 1000 * NetworkMethods.GetHTMLRequestTimeout();
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = postData.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = new CookieContainer();
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(postData);
            writer.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());

            string tmp = reader.ReadToEnd();
            CookieCollection cookies = response.Cookies;
            response.Close();
            return tmp;
        }

        public static string PostAndReturnHTML(string url, string postData)
        {
            try
            {
                Uri uri = new Uri(url);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                //request.Timeout = 1000 * 60 * 2;
                //request.Timeout = 1000 * NetworkMethods.GetHTMLRequestTimeout();
                request.Method = WebRequestMethods.Http.Post;
                request.ContentLength = postData.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = new CookieContainer();
                StreamWriter writer = new StreamWriter(request.GetRequestStream());
                writer.Write(postData);
                writer.Close();
                WebResponse httpWResponse = null;
                httpWResponse = request.GetResponse();
                Stream receiveStream = httpWResponse.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream);
                string tmp = reader.ReadToEnd();
                //CookieCollection cookies = response.Cookies;
                httpWResponse.Close();
                receiveStream.Close();
                return tmp;
            }
            catch (Exception ekkk)
            {
                return "";
            }
        }

        public static CookieCollection PostAndReturnCookies(string url, string data)
        {
            string postData = data;
            Uri uri = new Uri(url);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            //request.Timeout = 1000 * NetworkMethods.GetHTMLRequestTimeout();
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = postData.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = new CookieContainer();
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(postData);
            writer.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();
            CookieCollection cookies = response.Cookies;

            return cookies;
        }

        public static string GetPara(string url, string queryName)
        {
            string ret = "";
            Uri uri = new Uri(url);
            string query = uri.Query.Replace("?", "");
            foreach (string nameAndValue in query.Split('&'))
            {
                string name = nameAndValue.Split('=')[0];
                string value = nameAndValue.Split('=')[1];
                if (name.ToLower() == queryName.ToLower()) return value;
            }
            return ret;
        }

        public static string SetPara(string url, string name, string value)
        {
            if (!url.Contains('?')) return url + "?" + name + "=" + value;
            if (url.EndsWith("?")) return url + name + "=" + value;
            string ret = url.Split('?')[0];
            Uri uri = new Uri(url);
            string query = uri.Query.Replace("?", "");
            if (query.ToLower().Contains(name.ToLower() + "="))
                foreach (string nameAndValue in query.Split('&'))
                {
                    string name1 = nameAndValue.Split('=')[0];
                    if (name.ToLower() == name1.ToLower()) ret += ret.Contains('?') ? "&" + name + "=" + value : "?" + name + "=" + value;
                    else ret += ret.Contains('?') ? "&" + nameAndValue : "?" + nameAndValue;
                }
            else ret += "?" + query + "&" + name + "=" + value;
            return ret;
        }

        public static List<HtmlNode> Select(string html, string selector)
        {
            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);
            var document = htmlDocument.DocumentNode;
            return document.QuerySelectorAll(selector).ToList();
        }

    }
}